package com.jee.support.classloader;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileClassLoader extends ClassLoader{
	
	public Class<?> findClass(String name){
		byte[]data = loadClassData(name);
		return defineClass("com.jee.support.classloader.MyApp", data, 0, data.length);
	}
	
	private byte[]loadClassData(String name){
		FileInputStream fis = null;
		byte[]data = null;
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			fis = new FileInputStream(new File("/Users/feize/Documents/workspace-core/jeesupport/target/classes/com/jee/support/classloader/MyApp.class"));
			int ch = 0;
			while((ch = fis.read()) != -1){
				baos.write(ch);
			}
			data = baos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				baos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return data;
	}
}
